import { Injectable, inject } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs';
import {
  Category,
  CreateCategoryRequest,
  UpdateCategoryRequest,
  CategoryListResponse,
  QueryParams,
  ApiResponse
} from '../models/blog.model';
import { AuthService } from './auth.service';

@Injectable({
  providedIn: 'root'
})
export class CategoryService {
  private readonly BASE_URL = 'http://localhost:8000/api/v1';
  private http = inject(HttpClient);
  private authService = inject(AuthService);

  /**
   * 获取分类列表
   */
  getCategories(params?: QueryParams): Observable<CategoryListResponse> {
    let httpParams = new HttpParams();

    if (params?.page) {
      httpParams = httpParams.set('page', params.page.toString());
    }
    if (params?.size) {
      httpParams = httpParams.set('size', params.size.toString());
    }
    if (params?.q) {
      httpParams = httpParams.set('q', params.q);
    }

    return this.http.get<CategoryListResponse>(`${this.BASE_URL}/category`, { params: httpParams });
  }

  /**
   * 获取单个分类
   */
  getCategoryById(id: string): Observable<ApiResponse<{ category: Category }>> {
    return this.http.get<ApiResponse<{ category: Category }>>(`${this.BASE_URL}/category/${id}`);
  }

  /**
   * 创建分类
   */
  createCategory(data: CreateCategoryRequest): Observable<ApiResponse> {
    return this.http.post<ApiResponse>(`${this.BASE_URL}/category`, data);
  }

  /**
   * 更新分类
   */
  updateCategory(id: string, data: UpdateCategoryRequest): Observable<ApiResponse<{ category: Category }>> {
    return this.http.put<ApiResponse<{ category: Category }>>(`${this.BASE_URL}/category/${id}`, data);
  }

  /**
   * 删除分类
   */
  deleteCategory(id: string): Observable<ApiResponse> {
    return this.http.delete<ApiResponse>(`${this.BASE_URL}/category/${id}`);
  }

  /**
   * 获取认证请求头
   */
}
